/*
 *  Group.cpp
 *  TubeTruck
 *
 *  Created by Aeturnum on 5/22/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "Group.h"

using namespace std;
using namespace TTP;

/*
std::vector<byte> address;
std::map<uint32_t,Identity*> participants;
Crypt::RSA mGroupRSA;
Crypy::BlowFish mGroupBlowfish;
*/

Group::Group(){}
Group::Group(const Address& address, std::vector<byte>& pubKey) : mAddress(address) {
   mGroupRSA.setPublicKey(pubKey);
}

// accessors
std::vector<byte> Group::getAddress() const {
   return mAddress;
}

Crypt::Identity* Group::getIdentity(uint32_t idHash) {
   map<uint32_t, Crypt::Identity*>::iterator loc =  mParticipants.find(idHash);
   if(loc == mParticipants.end()) {
      throw identity_exception("Identity:getIdentity: Requested idHash does not exist");
   } else {
      return loc->second;
   }
}

bool Group::containsIdentity(uint32_t idHash) {
   map<uint32_t, Crypt::Identity*>::iterator loc =  mParticipants.find(idHash);
   if(loc == mParticipants.end()) {
      return false;
   } else {
      return true;
   }   
}

std::vector<Crypt::Identity*> Group::getAllIdentities() {
   std::vector<Crypt::Identity*> retVal;
   std::map<uint32_t, Crypt::Identity*>::iterator itr = mParticipants.begin();
   
   while(itr != mParticipants.end()) {
      retVal.push_back(itr->second);
      ++itr;
   }
   return retVal;
}

uint32_t Group::getLowestID() {
   std::map<uint32_t, Crypt::Identity*>::iterator itr = mParticipants.begin();
   return itr->first;
}

Crypt::RSA& Group::getGroupRSA() {
   return mGroupRSA;
}

Crypt::BlowFish Group::getGroupBlowfish() {
   return mGroupBlowfish;
}

// modifers
void Group::setAddress(const Address& address) {
   mAddress = address;
}

void Group::addIdentity(Crypt::Identity* i) {
   mParticipants[i->getIdHash()] = i;
}
